<?php
namespace app\seller\controller;
use app\common\controller\Seller;
use app\common\model\Company;
use app\common\model\Visit;
use app\common\model\Member;
use app\common\model\Order;
use app\common\model\Amount;
use think\Cache;
class Index extends Seller
{
    
    public function index()
    {
        $this->redirect('/index/analysis');
//        return $this->fetch();
    }
    
    public function analysis(){
        
        $sellerId = $this->getSellerId();
        
        $seller = Member::get($this->getSellerId());
        $company = Company::get(['user_id'=>$this->getSellerId()]);
        $this->assign('company',$company);
        $this->assign('seller',$seller);
        //显示的是状态正常，而且分销等级大于0的用户
        $startTime = strtotime(date("Y-m-d"),time());//获取今天凌晨时间戳
        $endTime = $startTime+86400;//获取今天最后一秒的时间戳
    
        Cache::clear();
        //近一个月订单
        if(Cache::get('orderMonthTotalNum'.$sellerId)){
            $orderTotalNum = Cache::get('orderMonthTotalNum'.$sellerId);
        }else{
            $filter0['create_time'] = array(array('EGT',$startTime-30*86400),array('ELT',$endTime),'and');
            $filter0['status'] = array('EGT',1);
            $orderMonthTotalNum = Order::where($filter0)->sum('amount');
            Cache::set('orderMonthTotalNum'.$sellerId,$orderMonthTotalNum,3600*24);
        }
    
    
        //订单总额
        if(Cache::get('orderTotalNum'.$sellerId)){
            $orderTotalNum = Cache::get('orderTotalNum'.$sellerId);
        }else{
            $orderTotalNum = Order::where('status','gt',0)->sum('amount');
            Cache::set('orderTotalNum'.$sellerId,$orderTotalNum,3600*24);
        }
    
        //今天登陆过的
        
        $filter1['create_time'] = array(array('EGT',$startTime),array('ELT',$endTime),'and');
        if(Cache::get('todayCustomerTotal'.$sellerId)){
            $todayCustomerTotal = Cache::get('todayCustomerTotal'.$sellerId);
        }else{
            $todayCustomerTotal = count(Visit::where($filter1)->select());
            Cache::set('todayCustomerTotal'.$sellerId,$todayCustomerTotal,3600*24);
        }
        
        $filter2['create_time'] = array(array('EGT',$startTime-7*86400),array('ELT',$endTime),'and');
        if(Cache::get('weekCustomerTotal'.$sellerId)){
            $weekCustomerTotal = Cache::get('weekCustomerTotal'.$sellerId);
        }else{
            $weekCustomerTotal = count(Visit::where($filter2)->select());
            Cache::set('weekCustomerTotal'.$sellerId,$weekCustomerTotal,3600*24);
        }
        
    
    
        $filter2['create_time'] = array(array('EGT',$startTime-30*86400),array('ELT',$endTime),'and');
        if(Cache::get('monthCustomerTotal'.$sellerId)){
            $monthCustomerTotal = Cache::get('monthCustomerTotal'.$sellerId);
        }else{
            $monthCustomerTotal = count(Visit::where($filter2)->select());
            Cache::set('monthCustomerTotal'.$sellerId,$monthCustomerTotal,3600*24);
        }
        
        
    
        //昨天订单数
        if(Cache::get('orderNumToday'.$sellerId)){
            $orderNumToday = Cache::get('orderNumToday'.$sellerId);
        }else{
            $filter['create_time'] = array(array('EGT',$startTime),array('ELT',$endTime),'and');
            $orderNumToday = Order::where( $filter)->count();
            Cache::set('orderNumToday'.$sellerId,$orderTotalNum,3600*24);
        }
    
        //七天内订单数量
        if(Cache::get('orderNumWeek'.$sellerId)){
            $orderNumWeek = Cache::get('orderNumWeek'.$sellerId);
        }else{
            $filter['create_time'] = array(array('EGT',$startTime-7*86400),array('ELT',$endTime),'and');
            $orderNumWeek = Order::where($filter)->count();
            Cache::set('orderNumWeek'.$sellerId,$orderTotalNum,3600*24*7);
        }
    
        //一个月订单数量
        if(Cache::get('orderNumMonth'.$sellerId)){
            $orderNumMonth = Cache::get('orderNumMonth'.$sellerId);
        }else{
            $filter['create_time'] = array(array('EGT',$startTime-30*86400),array('ELT',$endTime),'and');
            $orderNumMonth = Order::where($filter)->count();
            Cache::set('orderNumMonth'.$sellerId,$orderTotalNum,3600*24*30);
        }
        $this->assign('orderNumMonth',$orderNumMonth);
        $this->assign('orderNumWeek',$orderNumWeek);
        $this->assign('orderNumToday',$orderNumToday);
    
        $this->assign('orderMonthTotalNum',round($orderMonthTotalNum,2));
        $this->assign('orderTotalNum',round($orderTotalNum,2));
        $this->assign('todayCustomerTotal',$todayCustomerTotal);
        $this->assign('weekCustomerTotal', $weekCustomerTotal);
        $this->assign('monthCustomerTotal',$monthCustomerTotal);
        
        return $this->fetch();
        
        
    }
}
